import axios from 'axios';
import Vue from 'vue'
import MD5 from 'crypto-js/md5';
import store from "../store/index";
// axios.defaults.withCredentials = true;
const instance = axios.create({
    timeout: 5000,
});
const http = {
    app_key: 51275674,
    app_secret: '8f128cc885f311e6b7cc64006a3a2de6',
    app_source: 'xpqh',
    /**
     *
     * MD5加密
     * @param {any} obj
     * @returns {String} MD5加密后的字符串
     */
    MD5Params(obj) {
        //排序的函数
        var newkey = Object.keys(obj).sort();
        let newString = '';
        for (var i = 0; i < newkey.length; i++) {
            if (obj[newkey[i]] !== undefined && obj[newkey[i]] !== '') {
                newString = newString + newkey[i] + obj[newkey[i]]
            }
        }
        console.log(newString);
        //这里是签名转义，还有encodeURIComponent
        // newString = encodeURIComponent(this.app_secret + newString + this.app_secret);
        newString = encodeURI(this.app_secret + newString + this.app_secret);
        return MD5(newString).toString().toUpperCase();
    },
    /**
     * 获取签名
     * @param {any} params
     * @returns {String} MD5加密后的字符串
     */
    getSign(params) {

        params.app_key = this.app_key;
        params.timestamp = '2017-09-26 15:52:28';
        // console.log((params))
        return this.MD5Params(params)
    },
   /**
    *
    * @param {String} url
    * @param {any} body
    */
    post(url, body) {
        return new Promise((resole, reject) => {
            for (let key in body){
                if (body[key] === undefined || body[key] === '' || (/^\s+$/gi).test(body[key])) {
                    delete body[key]
                }
            }
            body.sign = this.getSign(body);
            let bodyArr = []
            for (let key in body) {
                //空格也删除掉
                if (body[key] !== undefined && body[key] !== '' && !(/^\s+$/gi).test(body[key])) {
                    //这里对每个数据转义，包括下面的等号一起,还有encodeURIComponent
                    // bodyArr.push(encodeURIComponent(key + '=' + body[key]))
                    bodyArr.push(encodeURI(key + '=' + body[key]))
                }
            }
            let postParams = url + '?' + bodyArr.join('&');
            return instance.post(postParams, '').then((data) => {
                if (data.data.code == -2) {
                    store.commit("logout");
                    store.commit("clearAllTags");
                    store.commit("clearOpenedSubmenu");
                    Vue.prototype.$Notice.error({
                        title: '提示',
                        desc: '长时间未操作，请重新登录'
                    });
                    window.location.href = window.location.href.split('/#/')[0] + '/#/login'
                } else {
                    resole(data.data);
                }
            }, (err) => {
                reject(err);
            }).catch((err) => {
                reject(err);
            });
        });
    },
     /**
    *
    * @param {String} url
    * @param {any} body
    */
    get(url, body) {
        return new Promise((resole, reject) => {
            let getData = ''
            if (body) {
                body.sign = this.getSign(body);
                let bodyArr = []
                for (let key in body) {
                    bodyArr.push(key + '=' + body[key])
                }
                getData = url + '?' + bodyArr.join('&');
            } else {
                getData = url
            }
            return instance.get(getData, '').then((data) => {
                resole(data.data);
            }, (err) => {
                // console.log(err);
                reject(err);

            }).catch((err) => {
                reject(err);
            });
        });
    },
    // send_jsonp: function(url, body) {
    //   return new Promise((resole, reject) => {
    //     let parmas = this.aesEncrypt(body);
    //     let postData = url + '?params=' + parmas;
    //     jsonp(postData, {
    //       timeout: 5000
    //     }, function(err, data) {
    //       console.log(err, data);
    //       if (err) {
    //         reject(err);
    //       } else {
    //         if (data.code === 1) {
    //           resole(data);
    //         } else {
    //           reject();
    //         }
    //       }
    //     });
    //   });
    //
    // }
    // let axios = axios.create({
    //   baseURL: `${API_BASE}/posts/`
    // });
};
export default http;
